﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Basic;
using Interface;

namespace Client
{
    /// <summary>
    /// Transform方法扩展
    /// </summary>
    public static class ExtendTransform
    {
        /// <summary>
        /// 获取组件（组件不存在时添加后返回）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tr"></param>
        /// <returns></returns>
        public static T GetOrAddComponent<T>(this Transform tr) where T : Component
        {
            if (tr == null)
            {
                Log.Error("ExtendTransform.GetOrAddComponent tr is null");
                return null;
            }

            T comp = tr.GetComponent<T>();
            if (comp == null)
            {
                return tr.gameObject.AddComponent<T>();
            }
            return comp;
        }
    }
}
